/**
 * 
 */
package com.cipe.cmrs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.model.Role;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.util.DateUtil;

/**
 * @author Incepio
 * 
 */
@Service
@Transactional
public class RoleService extends BaseService<Role> {

	@Autowired
	private UserService	userService;

	public Role saveRole(Role role, Integer userId) {
		if (role != null) {
			User user = userService.findById(userId);
			Role dbRole = findById(role.getId());
			if (dbRole == null) {
				dbRole = new Role();
				dbRole.setCreatedBy(user);
				dbRole.setCreationDate(DateUtil.getCurrentDate());
			}
			dbRole.setDisplayName(role.getDisplayName());
			dbRole.setUpdatedBy(user);
			dbRole.setUpdationDate(DateUtil.getCurrentDate());
			return merge(dbRole);
		}
		return null;
	}
}
